﻿using HZYProject.Core.Repositories;
using HZYProject.Domain;
using HZYProject.Service.ChapterServices;
using HZYProject.Service.QuestionServices;
using HZYProject.Web.Models;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace HZYProject.Web.Controllers
{
    /// <summary>
    /// 试题管理
    /// </summary>
    [Route("Question")]
    public class QuestionController : Controller
    {
        private readonly IRepository<Question, Guid> _repository;
        private readonly IQuestionService _questionService;
        private readonly IRepository<Chapter, Guid> _chapterRepository;

        public QuestionController(IRepository<Question, Guid> repository, IQuestionService questionService, IRepository<Chapter, Guid> chapterRepository)
        {
            _repository = repository;
            _questionService = questionService;
            _chapterRepository = chapterRepository;
        }

        #region 首页分页查询
        [HttpGet("")]
        public IActionResult Index()
        {
            var list = _chapterRepository.GetAllList();
            return View(list);
        }
        [HttpPost("")]
        public async Task<IActionResult> Index(UIQuestion c)
        {
            try
            {
                GetQuestionInput input = new GetQuestionInput { ID = c.QuestionID, Title = c.Title, CID = c.ChapterID };
                var data = await _questionService.GetPaginatedResult(input);
                return Json(new { code = 0, msg = "", data = data.Data, count = data.TotalCount });
            }
            catch (Exception ex)
            {
                return Json(new { code = 1, msg = "查询出错:" + ex.Message, data = new List<UIQuestion>(), count = 0 });
            }
        }
        #endregion

        #region 添加
        [HttpGet("add")]
        public IActionResult Add()
        {
            var list = _chapterRepository.GetAllList();
            return View(list);
        }
        [HttpPost("add")]
        public IActionResult Add(UIQuestion cla)
        {
            try
            {
                Question c = new Question();
                c.QuestionID = Guid.NewGuid();
                c.Title = cla.Title;
                c.ChapterID = cla.ChapterID;
                c.TypeName = cla.TypeName;
                c.Score = cla.Score;
                c.Content = cla.Content;
                c.Option = cla.Option;
                _repository.Insert(c);
                return Json(new { success = true, msg = "添加成功" });
            }
            catch (Exception ex)
            {
                return Json(new { success = false, msg = ex.Message });
            }
        }
        #endregion

        #region 修改
        [HttpGet("edit")]
        public IActionResult Edit(Guid id)
        {
            var obj = _repository.FirstOrDefault(x => x.QuestionID == id);
            ViewBag.Names = _chapterRepository.GetAllList();
            return View(obj);
        }
        [HttpPost("edit")]
        public IActionResult Edit(UIQuestion cla)
        {
            try
            {
                if (ModelState.IsValid)//obj在形成对象时，是否全部符合注解中的规定
                {
                    var obj = _repository.FirstOrDefault(x => x.QuestionID == cla.QuestionID);
                    obj.Title = cla.Title;
                    obj.ChapterID = cla.ChapterID;
                    obj.TypeName = cla.TypeName;
                    obj.Score = cla.Score;
                    obj.Content = cla.Content;
                    obj.Option = cla.Option;
                    _repository.Update(obj);
                    return Json(new { success = true, msg = "修改成功" });
                }
                else
                {
                    System.Text.StringBuilder sb = new System.Text.StringBuilder();
                    int i = 1;
                    foreach (var v in ModelState.Values)
                    {
                        foreach (var p in v.Errors)
                        {
                            sb.AppendFormat("{0}-{1};", i++, p.ErrorMessage);
                        }
                    }
                    return Json(new { success = false, msg = "服务端验证未通过," + sb });
                }
            }
            catch (Exception ex)
            {
                return Json(new { success = false, msg = ex.Message });
            }

        }
        #endregion

        #region 删除
        [HttpPost("remove/{id}")]
        public IActionResult Del(Guid id)
        {
            try
            {
                var obj = _repository.FirstOrDefault(x => x.QuestionID == id);
                _repository.Delete(obj);
                return Json(new { success = true, msg = "删除成功" });
            }
            catch (Exception ex)
            {
                return Json(new { success = false, msg = ex.Message });
            }
        }
        #endregion
    }
}
